﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Hosting;

namespace CoreEx.Wcf.Server
{
    public class VirtualSvcPathProvider : VirtualPathProvider
    {
        public override bool FileExists(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
                return true;


            return Previous.FileExists(virtualPath);
        }

        public override bool DirectoryExists(string virtualDir)
        {
            return true;
            return base.DirectoryExists(virtualDir);
        }

        public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            if (IsPathVirtual(virtualPath))
                return null;
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }


        private bool IsPathVirtual(string virtualPath)
        {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/services", StringComparison.InvariantCultureIgnoreCase);
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                VirtualFile test = new VirtualSvcFile(virtualPath, GetServiceContractName(virtualPath));
                return test;
            }

            return base.GetFile(virtualPath);
        }



        public string GetServiceContractName(string virtualPath)
        {
            return Path.GetFileNameWithoutExtension(virtualPath);
        }

       
    }
}
